package com.oron3.locker;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ofek.applicationbase.adapter.BaseAdapter;
import com.oron3.lock.R;
import com.oron3.locker.ToolController.ToolAdapter;

public class LockActivity extends ListActivity {
	public final static String KEY_INTENT_EXTRA_UC = "com.oron3.Lock.KEY_INTENT_EXTRA_UC";
	private ToolController toolController;
	private LockDatabase lockDatabase;
	private ArrayList<SecretID> secrets;
	private SecretAdapter adapter;

	public class SecretAdapter extends BaseAdapter {

		/**
		 * @param c
		 * @param rowLayoutResource
		 */
		public SecretAdapter(final Context c) {
			super(c, R.layout.list_item_title);
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			return secrets.size();
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(final int position) {
			return secrets.get(position);
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(final int position) {
			return secrets.get(position).sid;
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see com.ofek.applicationbase.adapter.BaseAdapter#adjustView(int,
		 * android.view.View)
		 */
		@Override
		public void adjustView(final int position, final View convertView) {
			final TextView title = (TextView) convertView.findViewById(R.id.title);
			final ImageButton delete = (ImageButton) convertView.findViewById(R.id.delete);
			final SecretID secretID = secrets.get(position);
			title.setText(secretID.title);
			delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(final View v) {
					lockDatabase.remove(secretID);
					notifyDataSetChanged();
				}
			});
			delete.setVisibility(toolController.getEditState() ? View.VISIBLE : View.GONE);
		}

		/*
		 * (non-Javadoc)
		 *
		 * @see android.widget.BaseAdapter#notifyDataSetChanged()
		 */
		@Override
		public void notifyDataSetChanged() {
			secrets = lockDatabase.getAllIDs();
			super.notifyDataSetChanged();
		}
	}

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Intent intent = getIntent();
		if (!intent.hasExtra(KEY_INTENT_EXTRA_UC)) {
			startActivity(new Intent(this, LoginActivity.class));
			finish();
			return;
		}
		setProgressBarVisibility(true);
		lockDatabase = LockDatabase.getInstance(intent.getStringExtra(KEY_INTENT_EXTRA_UC));
		secrets = lockDatabase.getAllIDs();
		setContentView(R.layout.activity_lock);
		toolController = new ToolController((ViewGroup) findViewById(R.id.tool));
		toolController.setSaveVisibility(false);
		adapter = new SecretAdapter(this);
		setListAdapter(adapter);
		toolController.registerToolListener(new ToolAdapter() {
			@Override
			public void onEditChecked(final boolean b) {
				adapter.notifyDataSetInvalidated();
			}

			@Override
			public void onAddClicked() {
				final Intent intent = new Intent(LockActivity.this, SecretFormActivity.class);
				intent.putExtra(KEY_INTENT_EXTRA_UC, getIntent().getStringExtra(KEY_INTENT_EXTRA_UC));
				startActivity(intent);
			}

		});

		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
				final SecretID item = (SecretID) parent.getItemAtPosition(position);
				final Intent intent = new Intent(LockActivity.this, SecretFormActivity.class);
				intent.putExtra(KEY_INTENT_EXTRA_UC, getIntent().getStringExtra(KEY_INTENT_EXTRA_UC));
				intent.putExtra(SecretFormActivity.KEY_INTENT_EXTRA_SECRET_TO_EDIT, item);
				startActivity(intent);
			}
		});
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.ListActivity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		lockDatabase.release();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		adapter.notifyDataSetChanged();
	}

}
